﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore;

namespace news.admin.cyt369.com.services.ORM
{
    public partial class NewsContext: Microsoft.EntityFrameworkCore.DbContext
    {
        public NewsContext()
        {
        }

        public NewsContext(DbContextOptions<NewsContext> options) : base(options)
        {

        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (Admin.CYT369.Com.Utility.SystemSettingService.Instance.news_admin_cyt369_com_connection_string.DBType == "SQLSERVER2008")
            {
                optionsBuilder.UseSqlServer(Admin.CYT369.Com.Utility.SystemSettingService.Instance.news_admin_cyt369_com_connection_string.ConnectionString).ReplaceService<Microsoft.EntityFrameworkCore.Query.IQueryTranslationPostprocessorFactory, Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServer2008QueryTranslationPostprocessorFactory>();
            }
            else
            {
                optionsBuilder.UseSqlServer(Admin.CYT369.Com.Utility.SystemSettingService.Instance.news_admin_cyt369_com_connection_string.ConnectionString);
            }
            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            #region documents
            builder.Entity<news.admin.cyt369.com.services.Models.UserDocCategory>(entity => {
                entity.ToTable("TB_UserDocCategory");
                entity.HasOne(c => c.Parent).WithMany(c => c.SubTypes).HasPrincipalKey(c => c.Id).HasForeignKey(c => c.ParentID);
            });

            builder.Entity<news.admin.cyt369.com.services.Models.Document>(entity => {
                entity.ToTable("TB_Document").HasKey(c => c.Id);
            });

            builder.Entity<news.admin.cyt369.com.services.Models.DocumentExt>(entity =>
            {
                entity.ToTable("TB_DocumentExt");
                entity.HasKey(c => c.DocID);
                entity.HasOne(c => c.Doc).WithOne(c => c.DocExt).HasForeignKey<news.admin.cyt369.com.services.Models.DocumentExt>(c => c.DocID);
            });

            builder.Entity<news.admin.cyt369.com.services.Models.DocAttachment>(entity =>
            {
                entity.ToTable("TB_DocAttachment");
                entity.HasOne(c => c.Doc).WithMany(c => c.Attachments).HasForeignKey(c => c.DocID);
            });

            builder.Entity<news.admin.cyt369.com.services.Models.DocumentAudit>(entity =>
            {
                entity.ToTable("TB_DocumentAudit");
                entity.HasOne(c => c.Doc).WithMany(c => c.DocumentAudits).HasForeignKey(c => c.DocID);
            });

            #endregion


        }


    }
}
